import { http } from "@/utils/http";

// 物料基本信息接口
export interface MaterialItem {
  id: number;
  name: string; // 物料名称
  brand: string; // 品牌
  specification: string; // 规格型号
  unit: string; // 单位
  remark?: string; // 备注
}

// 查询物料列表参数
export interface MaterialListParams {
  page?: number;
  limit?: number;
  name?: string; // 物料名称或品牌搜索
}

// 查询物料列表返回结果
export interface MaterialListResult {
  code: number;
  data: {
    list: MaterialItem[];
    total: number;
    endRow: number;
    hasNextPage: boolean;
    hasPreviousPage: boolean;
    isFirstPage: boolean;
    isLastPage: boolean;
  };
  message: string;
}

// 添加/修改物料参数
export interface MaterialPayload {
  name: string;
  brand: string;
  specification: string;
  unit: string;
  remark?: string;
}

// 创建物料返回结果
export interface MaterialCreateResult {
  code: number;
  data: object;
  message: string;
}

// 更新物料返回结果
export interface MaterialUpdateResult {
  code: number;
  data: object;
  message: string;
}

// 删除物料返回结果
export interface MaterialDeleteResult {
  code: number;
  data: object;
  message: string;
}

// 查询单个物料返回结果
export interface MaterialDetailResult {
  code: number;
  data: MaterialItem;
  message: string;
}

// 查询所有物料返回结果
export interface MaterialAllListResult {
  code: number;
  data: object;
  message: string;
}

// 批量删除物料返回结果
export interface MaterialBatchDeleteResult {
  code: number;
  data: object;
  message: string;
}

/**
 * 查询物料列表
 * @param data 查询参数
 * @returns 物料列表数据
 */
export const getMaterialList = (data: MaterialListParams) => {
  return http.request<MaterialListResult>("post", "material/list", { data });
};

/**
 * 添加物料
 * @param data 物料信息
 * @returns 创建结果
 */
export const createMaterial = (data: MaterialPayload) => {
  return http.request<MaterialCreateResult>("post", "material/add", { data });
};

/**
 * 修改物料信息
 * @param data 物料信息（包含id）
 * @returns 修改结果
 */
export const updateMaterial = (data: MaterialPayload & { id: number }) => {
  return http.request<MaterialUpdateResult>("post", "material/update", { data });
};

/**
 * 根据ID查询物料信息
 * @param id 物料ID
 * @returns 物料详细信息
 */
export const getMaterialById = (id: number) => {
  return http.request<MaterialDetailResult>("get", "material/find", { params: { id } });
};

/**
 * 查询所有物料信息
 * @returns 所有物料信息
 */
export const getAllMaterials = () => {
  return http.request<MaterialAllListResult>("get", "material/findAll");
};

/**
 * 删除物料信息
 * @param id 物料ID
 * @returns 删除结果
 */
export const deleteMaterial = (id: number) => {
  return http.request<MaterialDeleteResult>("get", "material/delete", { params: { id } });
};

/**
 * 批量删除物料信息
 * @param data 包含id数组的请求体
 * @returns 删除结果
 */
export const deleteBatchMaterial = (data: { ids: number[] }) => {
  return http.request<MaterialBatchDeleteResult>("post", "material/deleteBatch", { data });
};